package com.bluesmart.mia;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sys.DemoApp;
import com.util.Constant;
import com.util.ImageLoaderHelper;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;
import com.widget.CircleImageView;
import com.widget.SwipeListView;

import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class BabyCaregiversActivity extends Fragment implements
		OnClickListener {
	MyAdapter mAdapter;
	JSONArray mArray = new JSONArray();
	View mView = null;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,  
            Bundle savedInstanceState)   {
		
		mView = inflater.inflate(R.layout.activity_baby_caregivers, container, false);

		mArray = ((DemoApp) getActivity().getApplicationContext()).getSeletedBaby()
				.getJSONArray("caregiverlist");

		SwipeListView listview = (SwipeListView) mView.findViewById(R.id.listview);
		mAdapter = new MyAdapter();
		listview.setAdapter(mAdapter);
		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				JSONObject item = mArray.getJSONObject(arg2);
				Intent intent = new Intent(getActivity(),
						FamilyInfoActivity.class);
				intent.putExtra("info", JSON.toJSONString(item));
				startActivity(intent);
			}
		});

		mView.findViewById(R.id.invite).setOnClickListener(this);
		return mView;
	}

	class MyAdapter extends BaseAdapter implements OnClickListener {
		public int getCount() {
			return mArray.size();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			FoodHolder foodHold = null;

			if (convertView == null) {
				LayoutInflater mInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				foodHold = new FoodHolder();

				convertView = mInflater.inflate(R.layout.item_invited, null);
				foodHold.img = (CircleImageView) convertView.findViewById(R.id.img);
				foodHold.title = (TextView) convertView.findViewById(R.id.msg);
				foodHold.msg = (TextView) convertView.findViewById(R.id.msg_0);
				foodHold.delete = (TextView) convertView.findViewById(R.id.del);

				convertView.setTag(foodHold);
			} else {
				foodHold = (FoodHolder) convertView.getTag();
			}
			JSONObject item = mArray.getJSONObject(position);
			foodHold.title.setText(item.getString("caregiverNickName"));
			foodHold.msg.setText(item.getString("caregiverRole"));
			ImageLoaderHelper.displayImage(R.drawable.a37, foodHold.img,
					item.getString("caregiverUserImg"));

			foodHold.delete.setTag(String.valueOf(position));
			foodHold.delete.setOnClickListener(this);

			foodHold.img.setTag(String.valueOf(position));
			return convertView;
		}

		@Override
		public void onClick(View arg0) {
			showQuerySearch(arg0);
		}

		void showQuerySearch(final View view) {
			final Dialog mDialog = new Dialog(getActivity(), R.style.Dialog_Fullscreen);
			mDialog.setContentView(R.layout.dialog_query);
			mDialog.findViewById(R.id.left).setOnClickListener(
					new OnClickListener() {
						@Override
						public void onClick(View arg0) {
							mDialog.dismiss();
						}
					});
			mDialog.findViewById(R.id.right).setOnClickListener(
					new OnClickListener() {
						@Override
						public void onClick(View arg0) {
							mDialog.dismiss();
							sendHttpRequest(view);
						}
					});
			((TextView) mDialog.findViewById(R.id.content))
					.setText(getString(R.string.delete_tip));
			mDialog.show();
		}

		private void sendHttpRequest(View arg0) {
			final int position = Integer.valueOf((String) arg0.getTag());
			
			MgqDataHandler loginHandler = new MgqDataHandler(getActivity(), true, false) {
				@Override
				public void onSuccess(String response) {
					JSONObject ruser = JSON.parseObject(response);
					if (ruser.getString("resCode").equals("1000")) {
						ToastUtil.showToastWithOkPic(R.string.success);
						mArray.remove(position);
						mAdapter.notifyDataSetChanged();
						
						((DemoApp)getActivity().getApplicationContext()).sendGetBabyRequest(getActivity());
						
					} else if (ruser.getString("resCode").equals("2000")) {
						ToastUtil.showToastWithAlertPic(R.string.error_2000);
					} else if (ruser.getString("resCode").equals("3000")) {
						ToastUtil.showToastWithAlertPic(R.string.error_3000);
					} else {
					}
				}
			};

			JSONObject item = mArray.getJSONObject(position);

			JSONObject params = new JSONObject();
			params.put("mia_un", PreferencesUtils.getString(getActivity(), "username"));
			params.put("pwd", PreferencesUtils.getString(getActivity(), "password"));
			params.put("caregiverName", item.getString("caregiverName"));
			params.put("handType", "delete");

			MgqRestClient.postBody(Constant.SERVER_URL
					+ "/sw/CaregiverHandler.ashx", JSON.toJSONString(params),
					loginHandler);
		}
	}

	public final class FoodHolder {
		public CircleImageView img;
		public TextView title;
		public TextView msg;
		public TextView delete;
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.invite:
			startActivity(new Intent(getActivity(), BabyInviteActivity.class));
			break;

		case R.id.left:
			break;
		}
	}
}
